package org.jfonia.musicxml;

import java.io.IOException;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Helper class to read xml with a local DTD (offline)
 * 
 * @author wijnand.schepens@gmail.com
 */
public class SimpleEntityResolver implements org.xml.sax.EntityResolver
{
	private String localDTDPath;
	
	/**
	 * 
	 * @param localDTDPath location of local DTD's. If null, default location (specified in xml-file) is used, possibly through http
	 */
	public SimpleEntityResolver(String localDTDPath)
	{
		this.localDTDPath = localDTDPath;
	}
	
	public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
	{
		if (localDTDPath == null)
			return null; 	// default resolver
		else
		{
			// redirect to local DTD :

			int i = systemId.lastIndexOf('/');
			String dtd = systemId.substring(i + 1);
			// System.out.println("dtd: " + dtd);

			return new InputSource("file:" + localDTDPath + "/" + dtd);
		}
	}

}
